package com.android.gl2jni.filter;

import android.content.Context;
import android.opengl.Matrix;

/**
 * Create time: 2018/9/26.
 */
public class ScaleFilterProgram extends TextureFilterProgram {
  private static final float SCALE = 0.3f;

  private int mFrames;

  private int mMaxFrames = 14;

  private int mMiddleFrames = mMaxFrames / 2;

  private float[] mScaleMatrix = new float[16];

  public ScaleFilterProgram(Context context) {
    super(context, ProgramType.TEXTURE_EXT);
  }

  @Override
  protected void onDraw(int firstVertex, int vertexCount) {
    Matrix.setIdentityM(mScaleMatrix, 0);
    float progress;
    if (mFrames <= mMiddleFrames) {
      progress = mFrames * 1.0f / mMiddleFrames;
    } else {
      progress = 2f - mFrames * 1.0f / mMiddleFrames;
    }
    float scale = 1f + SCALE * progress;
    Matrix.scaleM(mScaleMatrix, 0, scale, scale, scale);
    updateMVPMatrix(mScaleMatrix);
    mFrames++;
    if (mFrames > mMaxFrames) {
      mFrames = 0;
    }
    super.onDraw(firstVertex, vertexCount);
  }

}
